/**
 * 将wx相关api封装成Promise化
 */

const onAsyncApis=[
    'onSocketOpen',
    'onSocketError',
    'onSocketMessage',
    'onSocketClose',
    'connectSocket',
    'sendSocketMessage',
    'closeSocket',
]


const Wo=onAsyncApis.reduce((prev,cur)=>{

    if(cur.indexOf('on')==-1) //这种才有状态回调
    prev[cur]=options=>new Promise((resolve,reject,completed)=>{
       //options为对应需要传递的参数键值队

        wx[cur]({
            ...options,
            success:res=>{resolve(res)},
            fail:res=>{reject(res)},
            complete:res=>{completed(res)}
        })
    }).catch(e=>{console.error(e.toString())})
    else prev[cur]=wx[cur]

    return prev

},{})


module.exports=Wo